package com.twetec.cms.service.system.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twetec.cms.model.po.system.RolePermissionPO;
import com.twetec.cms.model.vo.system.RolePermissionVO;
import com.twetec.cms.service.system.RolePermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.twetec.cms.dao.mapper.system.RolePermissionMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色和权限表 服务实现类
 * </p>
 *
 * @author zhxl
 * @since 2020-10-12
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermissionPO> implements RolePermissionService {

    @Override
    public boolean deleteByRoleId(Long roleId) {
        return baseMapper.deleteByRoleId(roleId);
    }

    @Override
    public boolean create(List<RolePermissionVO> rolePermissionVOList, Long roleId) {
        // 1.先删除数据库中所有与该角色相关的权限和角色相关信息
        boolean deleteResult = this.deleteByRoleId(roleId);

        // 2.判断数据是否合法
        if(null == rolePermissionVOList || rolePermissionVOList.isEmpty()){
            return false;
        }
        List<RolePermissionPO> rolePermissionPOList = new ArrayList<>();
        for (RolePermissionVO rolePermissionVO : rolePermissionVOList){
            RolePermissionPO rolePermissionPO = new RolePermissionPO();
            BeanUtils.copyProperties(rolePermissionVO, rolePermissionPO);
            rolePermissionPOList.add(rolePermissionPO);
        }

        boolean result = this.saveBatch(rolePermissionPOList);
        return result;
    }
}
